import { Component, OnInit, Input, EventEmitter, ViewChild, DoCheck, NgModule, Inject } from '@angular/core';
import { Router, ActivatedRoute } from '@angular/router';
import { ConmonService } from "../../service/common.service";
import { DOCUMENT } from '@angular/common';
declare var Snap: any, mina: any;
// declare var $: any;
import * as $ from 'jquery';
@Component({
  selector: 'app-person-tatal',
  templateUrl: './person-tatal.component.html',
  styleUrls: ['./person-tatal.component.scss']
})
@NgModule({
  providers: [
    ConmonService
  ]
})
export class PersonTatalComponent implements OnInit {
  private isapply: boolean = true;
  private iswork: boolean = false;
  private tokenObj: any = {};
  private domainVal: string = "afcat.com.cn";
  private url: string = "http://crmdev.afcat.com.cn:8001/psc/CRMDEV/EMPLOYEE/CRM/s/WEBLIB_SI_D.CRM_SI_HTTP.FieldFormula.IScript_CRM_Service";
  private dataList: any;
  private flag: any;
  private applyPages: number = 1;
  private ministryPages: number = 1;
  private arrList: any;
  private totalPages: any;
  private totalData: any;
  // private employId:string = '';
  tabCub(val) {
    if (val === '我的报工') {
      this.isapply = false;
      this.iswork = true;
      this.ministryPages = 1;
      this.getMethod('MyDailyWork');
    } else {
      this.isapply = true;
      this.iswork = false;
      this.applyPages = 1;
      this.getMethod("myApplyWorkInfo");
    };
  };
  //获取列表新的数据
  getMethod(serviceName) {
    // let d = new Date();
    // // d.setTime(d.getTime() + (1 * 24 * 60 * 60 * 1000));
    // d.setTime(d.getTime() + (5 * 1000));
    // let expires = "expires=" + d.toUTCString();
    // document.cookie = "ATOKEN=" + this.tokenObj + ";domain=" + this.domainVal + ";path=/;" + expires + ";";
    let _this = this;
    $.ajax({
      async: false, // 同步请求 false，异步请求 true
      type: "POST", // 请求类型，POST、GET
      xhrFields: {
        withCredentials: true
      },
      crossDomain: true,
      url: _this.service.url, // 请求的地址
      // data: { crmData: "{'head': {'ServName':" + "'" + serviceName + "'" + ",'SeqNo':'123123123123123123123123','employId':'000123'},'body':{'AccessPages':'1'}}" },
      data: { crmData: "{'head': {'ServName':" + "'" + serviceName + "'" + ",'SeqNo':'123123123123123123123123','employId':"+"'"+_this.service.employId+"'"+"},'body':{'AccessPages':'1'}}" },
      dataType: 'text',
      success: function (responseData) {
        responseData = JSON.parse(responseData);
        console.log(responseData);
        //判断是我的申请还是我的报工
        if (serviceName === 'myApplyWorkInfo') {
          _this.totalData = responseData;
          _this.totalPages = responseData.body.totalPages;
          var arr = responseData.body.myApplyWorkInfo;
        } else {
          _this.totalData = responseData;
          _this.totalPages = responseData.body.totalPages;
          var arr = responseData.body.myDailyWorkInfo;
        };
        _this.dataList = [];
        _this.dataList = _this.dataList.concat(arr);
        //按照报工日期排序进行展示
        for (var i = 0; i < arr.length - 1; i++) {
          console.log(arr[i].WorkDate);
          for (var j = i + 1; j < arr.length; j++) {
            if (arr[i].WorkDate < arr[j].WorkDate) {
              var baba = arr[j];
              arr[j] = arr[i];
              arr[i] = baba;
            };
          };
        };
        // _this.dataList = responseData;
      },
      error: function (XMLHttpRequest, textStatus, errorThrown) {
        // 处理请求发生异常
        console.log("请求发生异常！" + textStatus + errorThrown);
      },
    });
  };
  constructor(private router: Router, private service: ConmonService, private routerIonfo: ActivatedRoute,@Inject(DOCUMENT) private document: any) {
    var dateObj = {};
    // this.flag = this.routerIonfo.snapshot.params["flag"];
    this.flag = this.routerIonfo.snapshot.params["flag"];
    // this.tokenObj = service.getToken(1, dateObj, 'myApplyWorkInfo', false);
    this.tokenObj = service.token;
    console.log(this.tokenObj);
    // this.getMethod('myApplyWorkInfo');
  }
  //跳转筛选页面
  public screeningMethod(flag): void {
    this.router.navigate(['/PersonScreening', flag]);
  };
  //我的申请上拉加载更多
  applyScroll(event){
    let _this = this;
    let scrollDiv = document.getElementById('apply');
    let oldScrollTop = scrollDiv.scrollTop;
    let scrollHeight = scrollDiv.scrollHeight;
    let clientHeight = scrollDiv.clientHeight;
    console.log(scrollHeight);
    if ((oldScrollTop + clientHeight >= scrollHeight) && (_this.applyPages < _this.totalPages)) {
      console.log(_this.applyPages, _this.totalPages);
      // _this.pages++;
      upCallback();
    }
    function upCallback() {
      if (_this.applyPages >= _this.totalPages) {
        _this.applyPages = _this.totalPages;
      } else {
        _this.applyPages++;
      }
      console.log('上拉');
      // if (_this.pages <= _this.totalPages) {
      console.log(_this.applyPages);
      var dateObj;
      if (_this.service.personScreeningPostData === undefined) {
        var serviceName = 'myApplyWorkInfo';
        dateObj = {
          AccessPages: _this.applyPages
        };
      } else {
        var serviceName = 'FiltrateMyApplyWork';
        dateObj = _this.service.personScreeningPostData;
        dateObj.AccessPages = _this.applyPages;
      }
      console.log(dateObj);
      // _this.tokenObj = _this.service.getToken(1, dateObj, 'backlogListInfo', true);
      _this.arrList = _this.service.withBodyData(dateObj, serviceName).body.myApplyWorkInfo;
      for (var key in _this.arrList) {
        _this.dataList.push(_this.arrList[key]);
      };
      var arr = _this.dataList;
      //按照报工日期排序进行展示
      for (var i = 0; i < arr.length - 1; i++) {
        for (var j = i + 1; j < arr.length; j++) {
          if (arr[i].WorkDate < arr[j].WorkDate) {
            var baba = arr[j];
            arr[j] = arr[i];
            arr[i] = baba;
          };
        };
      };
      // };
    };
  };
  //我的报工上拉加载更多
  ministryScroll (event){
    let _this = this;
    let scrollDiv = document.getElementById('ministry');
    let oldScrollTop = scrollDiv.scrollTop;
    let scrollHeight = scrollDiv.scrollHeight;
    let clientHeight = scrollDiv.clientHeight;
    console.log(scrollHeight);
    if ((oldScrollTop + clientHeight >= scrollHeight) && (_this.ministryPages < _this.totalPages)) {
      console.log(_this.ministryPages, _this.totalPages);
      // _this.pages++;
      upCallback();
    }
    function upCallback() {
      if (_this.ministryPages >= _this.totalPages) {
        _this.ministryPages = _this.totalPages;
      } else {
        _this.ministryPages++;
      }
      console.log('上拉');
      // if (_this.pages <= _this.totalPages) {
      console.log(_this.ministryPages);
      var dateObj;
      if (_this.service.personScreeningPostData === undefined) {
        var serviceName = 'MyDailyWork';
        dateObj = {
          AccessPages: _this.ministryPages
        };
      } else {
        var serviceName = 'FiltrateMyDailyWork';
        dateObj = _this.service.personScreeningPostData;
        dateObj.AccessPages = _this.ministryPages;
      }
      console.log(dateObj);
      // _this.tokenObj = _this.service.getToken(1, dateObj, 'backlogListInfo', true);
      _this.arrList = _this.service.withBodyData(dateObj, serviceName).body.myDailyWorkInfo;
      for (var key in _this.arrList) {
        _this.dataList.push(_this.arrList[key]);
      };
      var arr = _this.dataList;
      //按照报工日期排序进行展示
      for (var i = 0; i < arr.length - 1; i++) {
        for (var j = i + 1; j < arr.length; j++) {
          if (arr[i].WorkDate < arr[j].WorkDate) {
            var baba = arr[j];
            arr[j] = arr[i];
            arr[i] = baba;
          };
        };
      };
      // };
    };
  };
  //下拉刷新
  applyDownRefresh () {
    let _this = this;
    if (_this.isapply) {
      var idname = 'apply';
    } else {
      var idname = 'ministry';
    }
    if (document.getElementById(idname)) {
      var mescroll = new MeScroll(idname, {
        down: {
          callback: downCallback //下拉刷新的回调,别写成downCallback(),多了括号就自动执行方法了
        }
        // up: {
        //   callback: upCallback
        // }
      });
    };
    //下拉刷新的回调
    function downCallback() {
      console.log('下拉');
      // _this.getWorkAuditData();
      //数组去重方法
      function unique5(array) {
        var r = [];
        for (var i = 0, l = array.length; i < l; i++) {
          for (var j = i + 1; j < l; j++)
            if (array[i].SubmitNo === array[j].SubmitNo) j = ++i;
          r.push(array[i]);
        }
        return r;
      };
      if (_this.isapply) {
        var servName = 'myApplyWorkInfo';
      } else {
        var servName = 'MyDailyWork';
      };
      $.ajax({
        url: _this.service.url,
        async: true, // 同步请求 false，异步请求 true
        type: "POST", // 请求类型，POST、GET
        xhrFields: {
          withCredentials: true
        },
        crossDomain: true,
        dataType: 'json',
        // data: { crmData: "{'head': {'ServName':"+"'"+servName+"'"+",'SeqNo':'123123123123123123123123','employId':'000123'},'body':{'AccessPages':'1'}}" },
        data: { crmData: "{'head': {'ServName':"+"'"+servName+"'"+",'SeqNo':'123123123123123123123123','employId':"+"'"+_this.service.employId+"'"+"},'body':{'AccessPages':'1'}}" },
        success: function (data) {
          console.log(data);
          //联网成功的回调,隐藏下拉刷新的状态;
          mescroll.endSuccess(); //无参
          //设置数据
          // _this.dataList = [];
          let totalData = data;
          _this.totalPages = totalData.body.totalPages;
          if (_this.isapply) {
            _this.arrList = totalData.body.myApplyWorkInfo;
          } else {
            _this.arrList = totalData.body.myDailyWorkInfo;
          };
          //数组合并
          _this.dataList = _this.dataList.concat(_this.arrList);
          _this.dataList = unique5(_this.dataList);
          console.log(_this.dataList);
          var arr = _this.dataList;
          //按照报工日期排序进行展示
          for (var i = 0; i < arr.length - 1; i++) {
            for (var j = i + 1; j < arr.length; j++) {
              if (arr[i].WorkDate < arr[j].WorkDate) {
                var baba = arr[j];
                arr[j] = arr[i];
                arr[i] = baba;
              };
            };
          };
        },
        error: function (data) {
          //联网失败的回调,隐藏下拉刷新的状态
          console.log(data);
          mescroll.endErr();
        }
      });
    };
  };
  ministryDownRefresh(){
    let _this = this;
    if (document.getElementById('ministry')) {
      var mescroll = new MeScroll('ministry', {
        down: {
          callback: downCallback //下拉刷新的回调,别写成downCallback(),多了括号就自动执行方法了
        }
        // up: {
        //   callback: upCallback
        // }
      });
    };
    //下拉刷新的回调
    function downCallback() {
      console.log('下拉');
      // _this.getWorkAuditData();
      //数组去重方法
      function unique5(array) {
        var r = [];
        for (var i = 0, l = array.length; i < l; i++) {
          for (var j = i + 1; j < l; j++)
            if (array[i].SubmitNo === array[j].SubmitNo) j = ++i;
          r.push(array[i]);
        }
        return r;
      };
      if (_this.isapply) {
        var servName = 'myApplyWorkInfo';
      } else {
        var servName = 'MyDailyWork';
      };
      $.ajax({
        url: _this.service.url,
        async: true, // 同步请求 false，异步请求 true
        type: "POST", // 请求类型，POST、GET
        xhrFields: {
          withCredentials: true
        },
        crossDomain: true,
        dataType: 'json',
        // data: { crmData: "{'head': {'ServName':"+"'"+servName+"'"+",'SeqNo':'123123123123123123123123','employId':'000123'},'body':{'AccessPages':'1'}}" },
        data: { crmData: "{'head': {'ServName':"+"'"+servName+"'"+",'SeqNo':'123123123123123123123123','employId':"+"'"+_this.service.employId+"'"+"},'body':{'AccessPages':'1'}}" },
        success: function (data) {
          console.log(data);
          //联网成功的回调,隐藏下拉刷新的状态;
          mescroll.endSuccess(); //无参
          //设置数据
          // _this.dataList = [];
          let totalData = data;
          _this.totalPages = totalData.body.totalPages;
          if (_this.isapply) {
            _this.arrList = totalData.body.myApplyWorkInfo;
          } else {
            _this.arrList = totalData.body.myDailyWorkInfo;
          };
          //数组合并
          _this.dataList = _this.dataList.concat(_this.arrList);
          _this.dataList = unique5(_this.dataList);
          console.log(_this.dataList);
          var arr = _this.dataList;
          //按照报工日期排序进行展示
          for (var i = 0; i < arr.length - 1; i++) {
            for (var j = i + 1; j < arr.length; j++) {
              if (arr[i].WorkDate < arr[j].WorkDate) {
                var baba = arr[j];
                arr[j] = arr[i];
                arr[i] = baba;
              };
            };
          };
        },
        error: function (data) {
          //联网失败的回调,隐藏下拉刷新的状态
          console.log(data);
          mescroll.endErr();
        }
      });
    };
  }
  ngOnInit() {
    // document.domain = this.domainVal;
    this.dataList = [];
    console.log(this.applyPages);
    //根据筛选页面传回的参数来判断是否重新渲染数据
    console.log(this.service.personScreeningData);
    if (this.flag === 'true' && this.service.personScreeningData != undefined) {
      //筛选我的申请数据
      this.isapply = true;
      this.iswork = false;
      var arr = this.service.personScreeningData.body.myApplyWorkInfo;
      //按照报工日期排序进行展示
      for (var i = 0; i < arr.length - 1; i++) {
        for (var j = i + 1; j < arr.length; j++) {
          if (arr[i].WorkDate < arr[j].WorkDate) {
            var baba = arr[j];
            arr[j] = arr[i];
            arr[i] = baba;
          };
        };
      };
      // this.getMethod('myApplyWorkInfo');
      this.totalPages = this.service.personScreeningData.body.totalPages;
      this.dataList = this.service.personScreeningData.body.myApplyWorkInfo;
    } else if (this.flag === 'false' && this.service.personScreeningData != undefined) {
      //筛选我的报工数据
      this.isapply = false;
      this.iswork = true;
      // this.getMethod('MyDailyWork');
      var arr = this.service.personScreeningData.body.myDailyWorkInfo;
      //按照报工日期排序进行展示
      for (var i = 0; i < arr.length - 1; i++) {
        for (var j = i + 1; j < arr.length; j++) {
          if (arr[i].WorkDate < arr[j].WorkDate) {
            var baba = arr[j];
            arr[j] = arr[i];
            arr[i] = baba;
          };
        };
      };
      this.totalPages = this.service.personScreeningData.body.totalPages;
      this.dataList = this.service.personScreeningData.body.myDailyWorkInfo;
    } else if (this.flag === 'true' && this.service.personScreeningData == undefined) {
      // this.getMethod('myApplyWorkInfo');
      this.applyDownRefresh();
    } else if (this.flag === 'false' && this.service.personScreeningData == undefined) {
      this.isapply = false;
      this.iswork = true;
      this.ministryDownRefresh();
      // this.getMethod('MyDailyWork');
    };

  };
  ngAfterContentInit() {
    if (this.service.personScreeningData == undefined) {
      // this.applyDownRefresh();
      // this.ministryDownRefresh();
      this.ministryDownRefresh();
    }
    console.log(123123);
    // this.refeshapply();
    // this.refeshministry();
  };

}
